package org.destiny.sticky;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;

public class MainPage extends JFrame {
    private static final long serialVersionUID = 1L;
    private JEditorPane textPane;

    public MainPage() {
        super("stick note");
        this.setBounds(860, 140, 300, 600);
        initToolbar();
        textPane = new JEditorPane();
        textPane.setDocument(new HTMLEditorKit().createDefaultDocument());
        getContentPane().add(textPane, BorderLayout.CENTER);
        try {
            initTrayIcon();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setType(Type.UTILITY);
        this.setVisible(true);
        HTMLDocument doc = (HTMLDocument)textPane.getDocument();
        HTMLDocument.BlockElement root = (HTMLDocument.BlockElement) doc.getDefaultRootElement();
        try {
            doc.insertBeforeEnd(root.getElement(0), "<ul><li>你好xxx</ui></li>");
        } catch (BadLocationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void watchHtml(HTMLDocument.BlockElement parent){
        Element e;
        for(int i = 0;i<parent.getElementCount();i++){
            e = parent.getElement(i);
            System.out.println(e.getName());
            if(e instanceof HTMLDocument.BlockElement)
                watchHtml((HTMLDocument.BlockElement) e);
        }
    }
    
    public void initToolbar(){
        JToolBar toolBar = new JToolBar(SwingConstants.HORIZONTAL);
        final JToggleButton pinBtn = new JToggleButton(ResourceUtil.getIcon("pin.gif"),true);
        pinBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pinBtn.isSelected())
                    setAlwaysOnTop(true);
                else
                    setAlwaysOnTop(false);
            }
        });
        toolBar.add(pinBtn);
        toolBar.add(new JButton(ResourceUtil.getIcon("view_menu.gif")));
        toolBar.add(new JButton(ResourceUtil.getIcon("view_menu.gif")));
        toolBar.add(new JButton(ResourceUtil.getIcon("view_menu.gif")));
        getContentPane().add(toolBar,BorderLayout.NORTH);
    }

    public void initTrayIcon() throws AWTException {
        Image trayImg = ResourceUtil.getImage("tray.gif");
        TrayIcon trayIcon = new TrayIcon(trayImg, "sticky note");
        TrayMouseHandler trayMouseHandler = new TrayMouseHandler();
        trayIcon.addMouseListener(trayMouseHandler);
        trayIcon.addActionListener(trayMouseHandler);
        SystemTray.getSystemTray().add(trayIcon);

    }
    
    @Override
    public void setExtendedState(int state) {
        if((state & JFrame.ICONIFIED) == ICONIFIED )
            setVisible(false);
        else
            super.setExtendedState(state);
    }

    public static void main(String[] args) {
        System.out.println(SystemTray.getSystemTray().getTrayIconSize());
        try {
            UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainPage();
            }
        });

    }

    class TrayMouseHandler implements MouseListener, ActionListener {
        JDialog win;
        JPopupMenu popupMenu;

        public TrayMouseHandler() {
            win = new JDialog();
            win.setUndecorated(true);
            win.setSize(1, 1);

            popupMenu = new JPopupMenu();
            popupMenu.add(new JMenuItem("show window"));
            popupMenu.add(new JMenuItem("hide window"));
            popupMenu.add(new JMenuItem("other thing1"));
            popupMenu.add(new JMenuItem("other thing2"));
            popupMenu.add(new JMenuItem("other thing3"));
            popupMenu.add(new JMenuItem("exit"));
        }

        public void mouseReleased(MouseEvent e) {
            checkPopup(e);
        }

        public void mousePressed(MouseEvent e) {
            checkPopup(e);
        }

        public void mouseClicked(MouseEvent e) {
            checkPopup(e);
        }

        public void checkPopup(MouseEvent e) {
            if (!e.isPopupTrigger())
                return;
            win.setLocation(e.getXOnScreen(), e.getYOnScreen());
            win.setVisible(true);
            popupMenu.show(win, 0, -popupMenu.getPreferredSize().height);
        }

        public void mouseEntered(MouseEvent e) {
            checkPopup(e);
        }

        public void mouseExited(MouseEvent e) {
            checkPopup(e);
        }

        public void actionPerformed(ActionEvent e) {
            MainPage.this.setVisible(true);
        }
    }
}
